package web;

import Utils.WebUtils;
import bean.User;
import dao.UserDao;
import dao.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(value="/UserServlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");

        try {
            switch (action) {
                case "login":
                    login(req, resp);
                    break;
                case "register":
                    register(req, resp);
                    break;
                default:
                    // 如果没有匹配的动作，默认重定向到登录页面
                    resp.sendRedirect(req.getContextPath() + "/pages/user/login.jsp");
            }
        } catch (SQLException e) {
            // 设置错误消息并跳转回相应的页面
            req.setAttribute("msg", "操作失败: " + e.getMessage());
            if ("login".equals(action)) {
                req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            } else if ("register".equals(action)) {
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            } else {
                resp.sendRedirect(req.getContextPath() + "/pages/user/login.jsp");
            }
        }
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        // 创建用户对象并从请求参数中复制属性值
        User user = new User();
        WebUtils.copyParamToBean(req.getParameterMap(), user);
        // 创建用户的业务层对象
        UserDao userDao = new UserDaoImpl();
        User loginUser = userDao.login(user);

        // 判断login是不是一个null对象
        if (loginUser == null) {
            // 用户名或密码错误，设置错误消息并跳转回登录页面
            req.setAttribute("msg", "用户名或密码错误！");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        } else {
            // 否则表示用户名密码输入正确，保存用户信息到会话并跳转到登录成功页
            HttpSession session = req.getSession();
            session.setAttribute("user", loginUser);
            req.getRequestDispatcher("/List.jsp").forward(req, resp);
        }
    }

    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        // 创建用户对象并从请求参数中复制属性值
        User user = new User();
        WebUtils.copyParamToBean(req.getParameterMap(), user);

        // 创建用户的业务层对象
        UserDao userDao = new UserDaoImpl();

        // 检查用户名是否已存在
//        if (userService.existsByUsername(user.getUsername())) {
//            // 用户名已存在，设置错误消息并跳转回注册页面
//            req.setAttribute("msg", "用户名 [" + user.getUsername() + "] 已存在！");
//            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
//            return; // 结束方法执行
//        }

        // 调用业务层方法保存用户数据
        userDao.addUser(user);

        // 跳转到注册成功页
        req.getRequestDispatcher("/login.jsp").forward(req, resp);
    }
}
